package com.tlvip.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 计算出牌的最大数量
 * 4中颜色卡牌，红，黄，蓝，绿
 * 每张牌标有数字，数字范围0-9之间
 * 一个人抽到若干张牌，如果第一次出牌是红色2，
 * 那么接下来只能出红色牌
 * 或者其他颜色的2数字牌
 * 编写一程序，计算可以出掉牌的最大数量
 */
public class CardMaxNum {

    public static void main(String[] args) {
        // 发牌数量
        int cardNum = 0;
        Random random = new Random();
        // 随机产生一个数字5-9之
        while (!(cardNum > 5 && cardNum < 9)) {
            cardNum = random.nextInt(10);
        }
        System.out.println("发牌数量：" + cardNum);
        List<Card> cardList = initCardList(cardNum);
        // 输出牌
        cardList.forEach(o -> System.out.println(o.getColor().name() + ":" + o.getNo()));

        // 每种颜色的卡牌号分布情况
        int[] resultRed = new int[10];
        int[] resultYellow = new int[10];
        int[] resultBlue = new int[10];
        int[] resultGreen = new int[10];

        // 初始化卡牌颜色分布情况
        initCardColorDistribute(cardList, resultRed, resultYellow, resultBlue, resultGreen);

        // 重新标记卡牌号码分布情况
        redoCardNoDistribute(cardList, resultRed, resultYellow, resultBlue, resultGreen);


        int max = 0;
        // 统计第一次出红牌时，可以出牌的数量总和
        int rSum = 0;
        for (int num : resultRed) {
            rSum += num;
        }
        // 统计第一次出黄牌时，可以出牌的数量总和
        int ySum = 0;
        for (int num : resultYellow) {
            ySum += num;
        }
        // 统计第一次出蓝牌时，可以出牌的数量总和
        int bSum = 0;
        for (int num : resultBlue) {
            bSum += num;
        }
        // 统计第一次出绿牌时，可以出牌的数量总和
        int gSum = 0;
        for (int num : resultGreen) {
            gSum += num;
        }
        // 求出牌的最大数量
        if (rSum < ySum) {
            max = ySum;
        }
        if (max < bSum) {
            max = bSum;
        }
        if (max < gSum) {
            max = gSum;
        }
        System.out.println("max:" + max);
    }

    private static void redoCardNoDistribute(List<Card> cardList, int[] resultRed, int[] resultYellow, int[] resultBlue,
                                             int[] resultGreen) {
        for (Card card : cardList) {
            if (card.getColor() == Color.RED) {
                if (0 != resultYellow[card.getNo()]) {
                    resultYellow[card.getNo()]++;
                }
                if (0 != resultBlue[card.getNo()]) {
                    resultBlue[card.getNo()]++;
                }
                if (0 != resultGreen[card.getNo()]) {
                    resultGreen[card.getNo()]++;
                }
            }
            if (card.getColor() == Color.YELLOW) {
                if (0 != resultRed[card.getNo()]) {
                    resultRed[card.getNo()]++;
                }
                if (0 != resultBlue[card.getNo()]) {
                    resultBlue[card.getNo()]++;
                }
                if (0 != resultGreen[card.getNo()]) {
                    resultGreen[card.getNo()]++;
                }
            }
            if (card.getColor() == Color.BLUE) {
                if (0 != resultRed[card.getNo()]) {
                    resultRed[card.getNo()]++;
                }
                if (0 != resultYellow[card.getNo()]) {
                    resultYellow[card.getNo()]++;
                }
                if (0 != resultGreen[card.getNo()]) {
                    resultGreen[card.getNo()]++;
                }
            }
            if (card.getColor() == Color.GREEN) {
                if (0 != resultRed[card.getNo()]) {
                    resultRed[card.getNo()]++;
                }
                if (0 != resultYellow[card.getNo()]) {
                    resultYellow[card.getNo()]++;
                }
                if (0 != resultBlue[card.getNo()]) {
                    resultBlue[card.getNo()]++;
                }
            }
        }
    }

    private static void initCardColorDistribute(List<Card> cardList, int[] resultRed, int[] resultYellow,
                                                int[] resultBlue,
                                                int[] resultGreen) {
        for (Card card : cardList) {
            if (card.getColor() == Color.RED) {
                resultRed[card.getNo()]++;
            }
            if (card.getColor() == Color.YELLOW) {
                resultYellow[card.getNo()]++;
            }
            if (card.getColor() == Color.BLUE) {
                resultBlue[card.getNo()]++;
            }
            if (card.getColor() == Color.GREEN) {
                resultGreen[card.getNo()]++;
            }
        }
    }

    private static List<Card> initCardList(int cardNum) {
        Random random = new Random();
        List<Card> cardList = new ArrayList<>(cardNum);
        // 出牌
        for (int i = 0; i < cardNum; i++) {
            // 随机出卡的号码
            int cardNo = random.nextInt(10);
            // 随机出卡的颜色
            int colorRandom = random.nextInt(5);
            Color color = null;
            if (colorRandom == 0) {
                color = Color.RED;
            }
            if (colorRandom == 1) {
                color = Color.YELLOW;
            }
            if (colorRandom == 2) {
                color = Color.BLUE;
            }
            if (colorRandom == 3) {
                color = Color.GREEN;
            }
            if (color == null) {
                color = Color.RED;
            }
            Card card = new Card(cardNo, color);
            cardList.add(card);
        }
        return cardList;
    }

    /**
     * 卡牌信息
     */
    static class Card {
        private int no;
        private Color color;

        public Card(int no, Color color) {
            this.no = no;
            this.color = color;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }

        public Color getColor() {
            return color;
        }

        public void setColor(Color color) {
            this.color = color;
        }
    }

    /**
     * 四种颜色
     */
    private enum Color {
        RED, YELLOW, BLUE, GREEN;
    }
}
